﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.IO;

/// <summary>
/// 需要传递一个参数：Request.QueryString["ItemID"]，ItemID是记录的ID（GUID类型）
/// </summary>
public partial class Common_Upload : System.Web.UI.Page
{
    private List<string> files = new List<string> { "gif", "jpg" };
    private List<string> images = new List<string> { "txt", "doc", "xls", "ppt", "rar", "zip" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        UserGrid.DataSource = Attachment.GetList("ItemID='" + Request.QueryString["ItemID"] + "'");
        UserGrid.DataBind();
    }
    protected void ButtonUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload.PostedFile.ContentLength == 0)
        {
            Prompt.Text = "警告：请选择文件！";
            return;
        }
        if (FileUpload.PostedFile.ContentLength > 2048 * 1024)
        {
            Prompt.Text = "警告：文件大小必须小于2048K！";
            return;
        }
        if (!System.IO.Directory.Exists("Upload"))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath("../Upload"));
        }
        int lineIndex = FileUpload.PostedFile.FileName.LastIndexOf("\\");
        int dotIndex = FileUpload.PostedFile.FileName.LastIndexOf(".");
        string fileExtension = FileUpload.PostedFile.FileName.Substring(dotIndex + 1).ToLower();
        string fileName = Guid.NewGuid().ToString();
        AttachmentInfo atta = new AttachmentInfo();
        atta.FileID = Guid.NewGuid();
        atta.FileName = fileName;
        atta.FileExtension = fileExtension;
        atta.FileClass = "未知";
        if (files.Contains(fileExtension))
            atta.FileClass = "图片";
        if (images.Contains(fileExtension))
            atta.FileClass = "文件";
        atta.FileType = FileUpload.PostedFile.ContentType;
        atta.ItemID = new Guid(Request.QueryString["ItemID"]);
        atta.FileSize = FileUpload.PostedFile.ContentLength;
        atta.FilePath = "/Upload/" + fileName + "." + fileExtension;
        atta.OraginalName = FileUpload.PostedFile.FileName.Substring(lineIndex + 1, dotIndex - lineIndex - 1);
        atta.Description = Description.Text;
        Attachment.Insert(atta);
        FileUpload.PostedFile.SaveAs(Server.MapPath("../Upload/" + fileName + "." + fileExtension));

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "上传附件";
        log.ItemID = Request.QueryString["ItemID"];
        log.ItemName = "Attachments";
        log.ItemFullUrl = "Upload.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：文件上传成功！";
        BindGrid();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void UserGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        AttachmentInfo atta = Attachment.GetAttachment(new Guid(UserGrid.DataKeys[e.RowIndex].Value.ToString()));
        string fileName = Server.MapPath("../" + atta.FilePath);
        if (File.Exists(fileName))
        {
            FileInfo file = new FileInfo(fileName);
            file.IsReadOnly = false;
            file.Delete();
        }
        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "删除附件";
        log.ItemID = Request.QueryString["ItemID"];
        log.ItemName = "Attachments";
        log.ItemFullUrl = "Upload.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Attachment.Delete(new Guid(UserGrid.DataKeys[e.RowIndex].Value.ToString()));
        BindGrid();
    }
}
